1 #include "SegmentTree.cpp"
9 void build_random_tree(int n
= 100){
11 for (int i
=0; i
<n
; ++i
) t
.arr
[i
] = random() * (random() % 2 ? -1 : 1);
15 void check_query_correctness(int u
, int v
){
18 for (int k
=u
; k
<=v
; ++k
) if (t
.arr
[k
] < t
.arr
[index
]) index
= k
;
19 int q
= t
.query(u
, v
);
20 printf("Range [%d, %d]:\n", u
, v
);
21 printf(" Tree query: index = %d, element = %d\n", q
, t
.arr
[q
]);
22 printf(" Lineal query: index = %d, element = %d\n", index
, t
.arr
[index
]);
27 void check_random_queries(int times
= 100){
29 int u
= random() % t
.n
, v
= random() % t
.n
;
30 if (v
< u
) swap(u
, v
);
31 check_query_correctness(u
, v
);
35 void check_update_correctness(int where
, int what
){
36 vector
<int> old
= t
.arr
;
37 t
.update(where
, what
);
40 printf("Update [%d] = %d\n", where
, what
);
45 void check_random_updates(int times
= 100){
47 int where
= random() % t
.n
, what
= random() * (random() % 2 ? -1 : 1);
48 check_update_correctness(where
, what
);
49 //after an update, check some random queries to see if everyting remains fine.
50 check_random_queries();
54 void print_array(const vector
<int> &arr
){
55 for (int i
=0; i
<arr
.size(); ++i
) printf("%d ", arr
[i
]);
62 check_random_queries(2*n
);
63 check_random_updates();